package com.qinghong.gulimall.authserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.qinghong.common.utils.HttpUtils;
import com.qinghong.common.utils.R;
import com.qinghong.gulimall.authserver.feign.MemberFeignService;
import com.qinghong.gulimall.authserver.vo.MemberRespVo;
import com.qinghong.gulimall.authserver.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理社交登录请求
 */
@Slf4j
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {

        Map<String , String > headers = new HashMap<>();
        Map<String , String > querys = new HashMap<>();
        Map<String , String > map = new HashMap<>();
        map.put("client_id","2364007558");
        map.put("client_secret","54139acb9bae04f15981b60f31053def");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
        map.put("code",code);
        //1、根据code换取accessToken
        //https://api.weibo.com/oauth2/access_token?client_id=2364007558
        // &client_secret=54139acb9bae04f15981b60f31053def
        // &grant_type=authorization_code
        // &redirect_uri=http://auth.gulimall.com/oauth2.0/weibo/success&code=5b898f78d9f3b4146cd5c45dc5562dd4
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token",
                "post", headers, querys, map);

        // 2、处理
        if (response.getStatusLine().getStatusCode()==200){
            // 获取到了accessToken
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            // 知道当前是哪个社交用户
            // 第一次进入就注册账号信息
            // 登录或者注册这个社交用户
            R authlogin = memberFeignService.authlogin(socialUser);
            if (authlogin.getCode() == 0){
                MemberRespVo data = authlogin.getData("data", new TypeReference<MemberRespVo>() {
                });
                System.out.println("登录成功,用户信息"+data);
                log.info("登录成功,用户信息{}",data);
                // 1、第一次使用session 命令浏览器保存卡号
                // 以后浏览器访问那个网站就会带带上这个网站的cookie
                // 发卡的时候指定子域名 子域之间 作用也作用到父域
//                servletResponse.addCookie(new Cookie("JESSIONID","dadaa").setDomain());

                //  TODO 默认发的令牌 session=dadass 作用域为但当前域

                session.setAttribute("LoginUser",data);

                // 2.登录成功就跳回首页
                return "redirect:http://gulimall.com";
            }else {
                return "redirect:http://auth.gulimall.com/login.html";
            }

        }else {
            return "redirect:http://auth.gulimall.com/login.html";
        }

        // 2.登录成功就跳回首页
    }
}
